
package comment

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/comment"
    commentReq "github.com/flipped-aurora/gin-vue-admin/server/model/comment/request"
)

type CommentsService struct {}
// CreateComments 创建comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService) CreateComments(comments *comment.Comments) (err error) {
	err = global.GVA_DB.Create(comments).Error
	return err
}

// DeleteComments 删除comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService)DeleteComments(id string) (err error) {
	err = global.GVA_DB.Delete(&comment.Comments{},"id = ?",id).Error
	return err
}

// DeleteCommentsByIds 批量删除comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService)DeleteCommentsByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]comment.Comments{},"id in ?",ids).Error
	return err
}

// UpdateComments 更新comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService)UpdateComments(comments comment.Comments) (err error) {
	err = global.GVA_DB.Model(&comment.Comments{}).Where("id = ?",comments.Id).Updates(&comments).Error
	return err
}

// GetComments 根据id获取comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService)GetComments(id string) (comments comment.Comments, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&comments).Error
	return
}
// GetCommentsInfoList 分页获取comments表记录
// Author [yourname](https://github.com/yourname)
func (commentsService *CommentsService)GetCommentsInfoList(info commentReq.CommentsSearch) (list []comment.Comments, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&comment.Comments{})
    var commentss []comment.Comments
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&commentss).Error
	return  commentss, total, err
}
func (commentsService *CommentsService)GetCommentsPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
